package com.xmh.demo.controller;

import com.xmh.demo.entity.UserEntity;
import com.xmh.util.FastDFSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.csource.common.MyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaominghui
 * @Date 2020-07-13 13:55:53
 */
@Api(tags = "UserController")
@RestController
@RequestMapping(value = "/API/video")
@Transactional
@CrossOrigin
public class VideoController {

//	@Autowired
//	private UserService videoService;

    @ApiOperation(value = "视频播放接口")
    @GetMapping("/play")
    public void getStream(HttpServletResponse response) throws IOException, MyException {

        byte[] fdfsDownload = FastDFSUtil.fdfsDownload("C:\\Users\\sean\\Documents\\HBuilderProjects\\VueTest\\src\\1.MOV");

        response.getOutputStream().write(fdfsDownload);
    }

    public static byte[] getBytesByFile(String filePath) {
        try {
            File file = new File(filePath);
            //获取输入流
            FileInputStream fis = new FileInputStream(file);

            //新的 byte 数组输出流，缓冲区容量1024byte
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            //缓存
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            //改变为byte[]
            byte[] data = bos.toByteArray();
            //
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//
//    /**
//     * 保存数据接口
//     *
//     * @param jsonMap
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "/save", method = RequestMethod.POST)
//    @ApiOperation(value = "保存接口", notes = "参数 ： ")
//    @ResponseBody
//    public Map<String, Object> save(@RequestBody Map<String, Object> jsonMap) throws Exception {
//        UserEntity videoParameter = new UserEntity(jsonMap);
//
//        return videoService.add(videoParameter);
//    }
//
//    /**
//     * 根据给定数据条件删除数据，不一定根据id删除
//     *
//     * @param jsonMap
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "/delete", method = RequestMethod.POST)
//    @ApiOperation(value = "删除接口", notes = "参数 ： ")
//    @ResponseBody
//    public Map<String, Object> delete(@RequestBody Map<String, Object> jsonMap) throws Exception {
//        UserEntity videoParameter = new UserEntity(jsonMap);
//
//        return videoService.delete(videoParameter);
//    }
//
//    /**
//     * update修改接口，根据唯一id进行修改
//     *
//     * @param jsonMap
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "/update", method = RequestMethod.POST)
//    @ApiOperation(value = "修改接口", notes = "参数 ： ")
//    @ResponseBody
//    public Map<String, Object> update(@RequestBody Map<String, Object> jsonMap) throws Exception {
//        UserEntity videoParameter = new UserEntity(jsonMap);
//
//        return videoService.update(videoParameter);
//    }
//
//    /**
//     * 查找多条数据，条件可以为空，默认查找全部
//     *
//     * @param jsonMap <pre>
//     *                                                             	pageIndex 页数下标
//     *                                                             	pageNumber 每页数量
//     *                                                                            </pre>
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
//    @ApiOperation(value = "获取列表接口", notes = "参数 ： ")
//    @ResponseBody
//    public Map<String, Object> selectList(@RequestBody Map<String, Object> jsonMap) throws Exception {
//        Map<String, Object> map = new HashMap<String, Object>();
//
//        map.put("list", videoService.selectList(jsonMap));
//
//        return map;
//    }
//
//    /**
//     * 根据指定条件获取单条数据
//     *
//     * @param jsonMap
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping(value = "/get", method = RequestMethod.POST)
//    @ApiOperation(value = "获取单体数据接口", notes = "参数 ： ")
//    @ResponseBody
//    public Map<String, Object> get(@RequestBody Map<String, Object> jsonMap) throws Exception {
//        UserEntity videoParameter = new UserEntity(jsonMap);
//
//        Map<String, Object> map = new HashMap<String, Object>();
//
//        map.put("entity", videoService.get(videoParameter));
//
//        return map;
//    }

}
